<?php  
// Poscast - view.php, 24/02/2007

/**
 * Mise � jour de la strucutre SQL
 */

    require_once("../../config.php");
    require_once("lib.php");

    $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
    $a  = optional_param('a', 0, PARAM_INT);  // podcast ID
	$tab  = optional_param('tab', 'view', PARAM_ALPHA);  // action
	

    if ($id) {
        if (! $cm = get_record("course_modules", "id", $id)) {
            error("Course Module ID was incorrect");
        }
    
        if (! $course = get_record("course", "id", $cm->course)) {
            error("Course is misconfigured");
        }
    
        if (! $podcast = get_record("podcast", "id", $cm->instance)) {
            error("Course module is incorrect");
        }

    } else {
        if (! $podcast = get_record("podcast", "id", $a)) {
            error("Course module is incorrect");
        }
        if (! $course = get_record("course", "id", $podcast->course)) {
            error("Course is misconfigured");
        }
        if (! $cm = get_coursemodule_from_instance("podcast", $podcast->id, $course->id)) {
            error("Course Module ID was incorrect");
        }
    }

    
	
	if(is_allowed('mod/podcast:use', $course->id, $cm->id))
	{
	
		if (isset($SESSION->modform)) {   // Variables are stored in the session
			$form = $SESSION->modform;
			unset($SESSION->modform);
		} else {
			$form = (object)$_POST;
		}
		$delete = 'null';
		if(isset($_GET['delete']))
			$delete = $_GET['delete'];
			
		$recup = 'null';
		if(isset($_GET['recup']))
			$recup = $_GET['recup'];
			
		$ajouter = 'null';
		if(isset($_GET['ajouter']))
			$ajouter = $_GET['ajouter'];	
	
		require_login($course->id);
	
		add_to_log($course->id, "podcast", "view", "view.php?id=$cm->id", "$podcast->id");
	
	/// Print the page header
	
        // build navigation array #UOW
        $navlinks = array();

		$strpodcasts = get_string("modulenameplural", "podcast");
		$strpodcast  = get_string("modulename", "podcast");

        $navlinks[] = array('name' => $strpodcasts, 'link' => "index.php?id=$course->id", 'type' => 'activity');
        $navlinks[] = array('name' => $podcast->name, 'link' => "", 'type' => '');
        $navigation = build_navigation($navlinks);

		print_header("$course->shortname: $podcast->name", "$course->fullname",
                   $navigation,
                   "", "", true, update_module_button($cm->id, $course->id, $strpodcast),
                   navmenu($course, $cm));
	
	
	/// Print the main part of the page	
	
		// Create Item
		if(!isset($form->action))
            $form->action = "";//Pour éviter la notice.
		if ($form->action == "create" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			//La variable erreur �volue en fonction des tests.
			$erreur = 0;
			
			$podcast_item->id = 0;
			$podcast_item->id_podcast = $form->id_podcast;
			$podcast_item->pubdate = $form->pubdate;
			$podcast_item->date_html = $form->date_html;
					
			if(!strcmp($CFG->podcast_mode ,'UPLOAD_ONLY')){
						
				// Creation de l'item
				$podcast_item->title =  $form->title;
				$podcast_item->lien =  $form->lien;		
				$podcast_item->intro = $form->intro;
				$podcast_item->duration = $form->duration;
				$podcast_item->length = $form->length;
				
				if (!check_podcast_extension($podcast_item->lien)) 
					$erreur = 1;
					
				if (trim($podcast_item->title) == "") 
					$erreur = 2;
		
			}else {
				error("This feature is not accessible in this mode.");
			}	
		
			switch($erreur) {
			
				case 0:
					if(insert_record("podcast_structure", addslashes_object($podcast_item))) {
						unset($form);
						$tab = "view";
						echo "<p style='color:green;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('add_ok','podcast')."</p>";
						// XML
						if (make_xml_podcast($podcast))
							echo "<p style='color:green;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('xml_ok','podcast')."</p>";
						else
							echo "<p style='color:red;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('xml_error','podcast')."</p>";
					} else {
						$tab = "add";
						echo "<p style='color:red;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('errordb','podcast')."</p>";
					}
				break;
				
				case 1:
					$tab = "add";
					echo "<p style='color:red;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('badextension','podcast');
					echo " ".implode(", ",get_podcast_limit_mimetypes())."</p>";
				break;
				
				case 2:
					$tab = "add";
					echo "<p style='color:red;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('title_empty','podcast')."</p>";
				break;
				
				default;
			}
		}
		
		// Update Item
		if ($form->action == "update" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			// Creation de l'item
			$podcast_item->id = $form->id_item;
			$podcast_item->id_podcast = $form->id_podcast;
			$podcast_item->title =  $form->title;
			$podcast_item->lien = $form->lien;		
			$podcast_item->intro = $form->intro;
			$podcast_item->pubdate = $form->pubdate;
			$podcast_item->date_html = $form->date_html;
			$podcast_item->duration = $form->duration;
			$podcast_item->length = $form->length;	
		
			// V�rification et insertion dans la base
			$erreur = 0;
	
			if (!check_podcast_extension($podcast_item->lien)) 
				$erreur = 1;
			
			if (trim($podcast_item->title) == "") 
				$erreur = 2;
			
			switch($erreur) {
			
				case 0:
					if(update_record("podcast_structure", addslashes_object($podcast_item))) {
						unset($form);
						$tab = "view";
						echo "<p style='color:green;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('update_ok','podcast')."</p>";
						// XML
						if (make_xml_podcast($podcast))
							echo "<p style='color:green;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('xml_ok','podcast')."</p>";
						else
							echo "<p style='color:red;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('xml_error','podcast')."</p>";
					} else {
						$tab = "add";
						echo "<p style='color:red;text-align:center;margin:0;font-size:0.8em;font-weight:bold;'>".get_string('errordb','podcast')."</p>";
					}
				break;
				
				case 1:
					$tab = "add";
					echo "<p style='color:red;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('badextension','podcast');
					echo " ".implode(", ",get_podcast_limit_mimetypes())."</p>";
				break;
				
				case 2:
					$tab = "add";
					echo "<p style='color:red;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('title_empty','podcast')."</p>";
				break;
				
				default;
			}
		}
		
		// Delete Item
		$action = optional_param('action', "view", PARAM_ALPHA);
		
		if ($action == "delete" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			$id_item = optional_param('id_item', 0, PARAM_INT);
		
			if(!delete_records_select("podcast_structure", "id = ".$id_item)) {
				error("Could not delete item podcast");
			} else {
				echo "<p style='color:green;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('del_ok','podcast')."</p>";
				unset($form);
				// XML 	
				if (make_xml_podcast($podcast))
					echo "<p style='color:green;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('xml_ok','podcast')."</p>";
				else
					echo "<p style='color:red;text-align:center;font-size:0.8em;font-weight:bold;'>".get_string('xml_error','podcast')."</p>";
			} 
			if (! get_records("podcast_structure", "id_podcast", $podcast->id))
				$tab = "view";
			
		}
		
		//Ici on traite les op�rations demand�es. Et n affiche les message en fonction
		if (isset($tab) && $tab == "manage" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			if($delete != "null")
				deleteItem($delete, $podcast, $id);
				
			if($recup != "null")
				recupItem($recup, $podcast, $id);
				
			if($ajouter != "null")
				ajouterItem($ajouter, $podcast, $id);
		
		}
		if(isset($tab) && $tab == 'view' && is_allowed('mod/podcast:edit', $course->id, $cm->id) && $delete != "null"){
			enleverItem($delete, $podcast, $id);
		}
			
		// Print Tabs for Teacher
	
		if (is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
	
			$tabs = array();
			$row  = array();
            if($CFG->podcast_mode === 'PCP'){
                if($CFG->podcast_capture)
                    $row[] = new tabobject('record', "view.php?id=$cm->id&amp;tab=record", get_string('record', 'podcast'));
                if($CFG->podcast_submit)
                    $row[] = new tabobject('upload', "view.php?id=$cm->id&amp;tab=upload", get_string('upload', 'podcast'));
                $row[] = new tabobject('view', "view.php?id=$cm->id&amp;tab=view", get_string('view', 'podcast')." ".get_string('modulename', 'podcast'));
                $row[] = new tabobject('manage', "view.php?id=$cm->id&amp;tab=manage", get_string('gestionItems', 'podcast'));
				$row[] = new tabobject('helpPcp', "view.php?id=$cm->id&amp;tab=helpPcp", get_string('help', 'podcast'));
            }elseif($CFG->podcast_mode === 'LINKING_ONLY'){
				$row[] = new tabobject('manage', "view.php?id=$cm->id&amp;tab=manage", get_string('gestionItems', 'podcast'));
                $row[] = new tabobject('view', "view.php?id=$cm->id&amp;tab=view", get_string('view', 'podcast')." ".get_string('modulename', 'podcast'));
				$row[] = new tabobject('helpLink', "view.php?id=$cm->id&amp;tab=helpLink", get_string('help', 'podcast'));
			}elseif($CFG->podcast_mode === 'UPLOAD_ONLY'){
				if (get_records("podcast_structure", "id_podcast", $podcast->id))
					$row[] = new tabobject('edit', "view.php?id=$cm->id&amp;tab=edit", get_string('edit', 'podcast') );
				$row[] = new tabobject('add', "view.php?id=$cm->id&amp;tab=add", get_string('add_item', 'podcast'));
                $row[] = new tabobject('view', "view.php?id=$cm->id&amp;tab=view", get_string('view', 'podcast')." ".get_string('modulename', 'podcast'));
				$row[] = new tabobject('helpUp', "view.php?id=$cm->id&amp;tab=helpUp", get_string('help', 'podcast'));
			}	
			$tabs[] = $row;
			print_tabs($tabs, $tab);
		}
	
	
		if (isset($tab) && $tab == "add" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			podcast_add_item($podcast, $id);
		
		}
		
		if (isset($tab) && $tab == "manage" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			podcast_manage_item($podcast, $id);
		
		}
		
		if (isset($tab) && $tab == "helpLink" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			helpLink($podcast, $id);
		
		}
		
		if (isset($tab) && $tab == "helpUp" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			helpUp($podcast, $id);
		
		}

       if (isset($tab) && $tab == "helpPcp" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {

			helpPcp($podcast, $id);

		}
		
		if (isset($tab) && $tab == "edit" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
		
			get_item_podcast($podcast, $id);
		
		}

        if (isset($tab) && $tab == "record" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {

			record_item_podcat($podcast, $id);

		}

        if (isset($tab) && $tab == "upload" && is_allowed('mod/podcast:edit', $course->id, $cm->id)) {

			upload_item_podcat($podcast, $id);

		}
	
	
		if ((is_allowed('mod/podcast:edit', $course->id, $cm->id) && $tab == "view") || !is_allowed('mod/podcast:edit', $course->id, $cm->id)) {
			get_podcast($podcast, $id);
		}
		
		/// Finish the page
		print_footer($course);

	}else{
		error(get_string('noauth', 'podcast').get_string('podcast:use', 'podcast'));
	}
?> 
